package com.yunao.service.report.esou;

import com.lowagie.text.*;
import com.yunao.commons.serachcore.core.bean.*;
import com.yunao.commons.serachcore.core.help.GroupField;
import com.yunao.commons.serachcore.core.help.MediaType;
import com.yunao.commons.serachcore.core.help.SearchFiled;
import com.yunao.entity.AloneReport;
import com.yunao.service.ReportService;
import com.yunao.service.report.DateUtil;
import esoucfg.DomainTrans;

import java.io.File;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ReportMonthly2 {

	private int id;
	private String name;
	private String word;
	private String query;
	private String organId;
	private AloneReport aloneReport;

	// 标题
	private String title;
	// 编号
	private String number;
	// 副标题
	private String subtitleLeft;
	private String subtitleRight;
	// 大数据综述
	private String overviewDesc;
	private String overviewDesc1;
	private String overviewDesc2;
	private String overviewDesc3;
	private String overviewImgs;
	// 大数据分布
	private String pieImgs;
	// 热点大数据
	private Clusters beans;
	private Clusters beansdl;
	private Clusters beanssdzw;
	private Clusters beanssd;
	private ReportService reportService;

	public ReportMonthly2(AloneReport aloneReport, ReportService reportService) {
		this.id = aloneReport.getId();
		this.name = aloneReport.getName();
		this.word = aloneReport.getWord();
		this.query = aloneReport.getQuery();
		this.organId = aloneReport.getSubjectOrganizationId();
		this.reportService = reportService;
		this.aloneReport = aloneReport;
	}

	public String createMailc() throws Exception {
		// 自动报告位置
		String filePath = ItextUtil.filepath(id);
		String docPath = filePath + System.currentTimeMillis() + ".doc";
		System.out.println("生成月报的路径："+docPath);
		File file = new File(docPath);
		if (!file.exists())
			file.createNewFile();
		// 自动报告的时间，最近一月
		long[] time = new long[2];
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);
		//time[1] = c.getTimeInMillis() - 1;
		c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1);
		//time[0] = c.getTimeInMillis();

		time=DateUtil.getSearchTime("30");
		System.out.println("即时报告区间："+time[0]+","+time[1]);

		// 创建 document
		Document doc = ItextUtil.open(docPath);
		// 月报标题
		title = name + "大数据月报";
		Paragraph headTitle = ItextUtil.createParagraph(title, ItextUtil.createStyle(ItextUtil.FAMILY_TITLE, 28,
				ItextUtil.COLOR_RED, Font.BOLD), Element.ALIGN_CENTER, 10, 10);
		doc.add(headTitle);
		// 月报编号
		c = Calendar.getInstance();
		c.setTime(new Date());
		number = "〔" + c.get(Calendar.YEAR) + "〕" + id + c.get(Calendar.MONTH) + "号";
		Paragraph headNumber = ItextUtil.createParagraph(number, ItextUtil.createStyle(ItextUtil.FAMILY_TITLE, 18,
				ItextUtil.COLOR_BLACK), Element.ALIGN_CENTER, 5, 5);
		doc.add(headNumber);
		// 月报副标题
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
		subtitleLeft = "云奥大数据";
		subtitleRight = dateFormat.format(new Date(time[0])) + "~" + dateFormat.format(new Date(time[1]));
		String subtitle = subtitleLeft + "                                 " + subtitleRight;
		Paragraph headSubtitle = ItextUtil.createParagraph(subtitle, ItextUtil.createStyle(ItextUtil.FAMILY_TITLE, 14,
				ItextUtil.COLOR_BLACK, Font.BOLD, Font.UNDERLINE), Element.ALIGN_CENTER, 5, 15);
		doc.add(headSubtitle);
		/*
		 * 大数据综述
		 */
		Paragraph overviewNav = ItextUtil.createParagraph("一、大数据综述", ItextUtil.createStyle(ItextUtil.FAMILY_TITLE, 18,
				ItextUtil.COLOR_BLACK, Font.BOLD), Element.ALIGN_LEFT, 10, 5);
		doc.add(overviewNav);
		Cubes cBeans = trend1(word, query, time);

		this.overviewDesc = "本月以“"+ word+"”为关键词。" +"对"+name +"的相关网情进行了实时监测，并对 "+ dateFormat.format(new Date(time[0])) + "至" + dateFormat.format(new Date(time[1]))
				+ "以来，互联网上的相关网情进行了整理分析。";
				//+ cBeans.getTotal() + "条。";
		// 大数据概况描述
		Paragraph overviewDesc = ItextUtil.createParagraph(this.overviewDesc, ItextUtil.createStyle(
				ItextUtil.FAMILY_TEXT, 12, ItextUtil.COLOR_BLACK, Font.NORMAL), Element.ALIGN_LEFT, 3, 3, 36);
		doc.add(overviewDesc);

		// 换行
		doc.add(ItextUtil.newline());

		Image img = null;
		String imgPath = null;
		int wzwf = 0 , zm = 0 , fm = 0 ;
		if(cBeans!=null&&cBeans.getSize()>0){
			imgPath = JFreeChartUtil.drawLine(cBeans, "一月大数据媒体类型趋势图", filePath);
			this.overviewImgs ="";
			this.overviewImgs += "," + imgPath;
			img = ItextUtil.createImage(imgPath, Element.ALIGN_CENTER, 450, 160);
			doc.add(img);
			doc.add(ItextUtil.newline());
			cBeans.sortByValue();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date parse = null;
			if(cBeans.getSize()>0)
				parse = simpleDateFormat.parse(cBeans.getData().get((int) (cBeans.getSize() - 1)).getKey());
			if(cBeans!=null&&cBeans.getSize()>0){
				this.overviewDesc1 ="文章分析关键词围绕：“"+word+"”，在分析时间范围内，共有"+cBeans.getTotal()+"篇相关信息。其中网情最高"
						+ "峰出现在"
						+ DateUtil.date2Str(parse, "yyyy年MM月dd日")
						+"，高峰时期"
						+ "共有"+cBeans.getData().get(0).getValue()+"篇相关信息。";
			}else{
				this.overviewDesc1 = "";
			}

			overviewDesc = ItextUtil.createParagraph(this.overviewDesc1, ItextUtil.createStyle(
					ItextUtil.FAMILY_TEXT, 12, ItextUtil.COLOR_BLACK, Font.NORMAL), Element.ALIGN_LEFT, 3, 3, 36);
			doc.add(overviewDesc);
			// 换行
			doc.add(ItextUtil.newline());
			cBeans = trend(word, query, time);
			imgPath = JFreeChartUtil.drawLineem(cBeans.getData(), "一月大数据正负面趋势图", filePath);
			this.overviewImgs += "," + imgPath;
			img = ItextUtil.createImage(imgPath, Element.ALIGN_CENTER, 450, 160);
			doc.add(img);
			doc.add(ItextUtil.newline());
			int len = (int)(cBeans.getSize().longValue());
			for (int i = len - 1; i >= 0; i--) {
				Cube bean = cBeans.getData().get(i);
				for (int j = 0; j <bean.getSubs().size(); j++) {
					switch (bean.getSubs().get(j).getKey()) {
						case "中性":wzwf += bean.getSubs().get(j).getValue() ;break;
						case "正面":zm += bean.getSubs().get(j).getValue() ;break;
						case "负面":fm += bean.getSubs().get(j).getValue() ;break;
					}
				}

			}
		}

		this.overviewDesc2 = "本月，正面网情有"+zm+"条，负面有"+fm+"条，中性有"+wzwf+"条。 ";
		overviewDesc = ItextUtil.createParagraph(this.overviewDesc2, ItextUtil.createStyle(
				ItextUtil.FAMILY_TEXT, 12, ItextUtil.COLOR_BLACK, Font.NORMAL), Element.ALIGN_LEFT, 3, 3, 36);
		doc.add(overviewDesc);
		Table table = new Table(2);
		Groups gBeans = group(word, query, time, GroupField.MEDIA_TYPE.toString());
		if(gBeans!=null){
			imgPath = JFreeChartUtil.drawPie1(gBeans, "一月大数据媒体类型分布图", filePath);
		}
		this.pieImgs = imgPath;
		img = ItextUtil.createImage(imgPath, Element.ALIGN_CENTER, 200, 150);
		Cell cell = new Cell();
		cell.setBorder(0);
		cell.add(img);
		table.addCell(cell);
//		Profile profile2 = new Profile("source.conf");
		DomainTrans d = DomainTrans.getInstance();
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(2);
		if(gBeans!=null&&gBeans.getSize()>0){
			gBeans.sortByValue();
			if(gBeans.getSize()>=3){
				this.overviewDesc3 =  "本月有关“"+word + "”的话题中，"+gBeans.getData().get(0).getKey()+"、"+gBeans.getData().get(1).getKey()
						+"和" + gBeans.getData().get(2).getKey()+ "三种媒体占比最高，其中"
						+gBeans.getData().get(0).getKey()+ "占"+ nf.format((double)gBeans.getData().get(0).getValue()/gBeans.getTotal()*100)+ "%；"
						+gBeans.getData().get(1).getKey()+ "占"+ nf.format((double)gBeans.getData().get(1).getValue()/gBeans.getTotal()*100)+ "%；"
						+gBeans.getData().get(2).getKey()+ "占"+ nf.format((double)gBeans.getData().get(2).getValue()/gBeans.getTotal()*100)+ "%。";
			}else{
				this.overviewDesc3 = "";
			}

			gBeans = group(word, query, time, GroupField.DOMAIN.toString());
			if (gBeans!=null){
				gBeans.sortByValue();
				imgPath = JFreeChartUtil.drawPie(gBeans.getData(), "一月大数据网站类型分布图", filePath);
			}

			this.pieImgs += "," + imgPath;
			img = ItextUtil.createImage(imgPath, Element.ALIGN_CENTER, 200, 150);
			cell = new Cell();
			cell.setBorder(0);
			cell.add(img);
			table.addCell(cell);
			table.setBorder(0);
			doc.add(table);
			if(gBeans.getSize()>=10){
				this.overviewDesc3 += "网站活跃度前三名分别是：";
				for (int i = 0; i < gBeans.getSize()& i<10; i++) {
					if (i<3) {
//					this.overviewDesc3 += profile2.getEncodeValue(gBeans.get(i).getKey());
						this.overviewDesc3 += d.parseEn(gBeans.getData().get(i).getKey());
					}
					if(i == 4)
						this.overviewDesc3 += "其他活跃度较高的网站包括：";

					if (i>3) {
//					this.overviewDesc3 += profile2.getEncodeValue(gBeans.get(i).getKey());
						this.overviewDesc3 += d.parseEn(gBeans.getData().get(i).getKey());
					}
					if (i == 3)
						this.overviewDesc3 += "，";
					else if (i == gBeans.getSize()-1)
						this.overviewDesc3 +="。";
					else
						this.overviewDesc3 += "，";
				}
			}
			if(!this.overviewDesc3.equals(""))
				this.overviewDesc3  =  this.overviewDesc3.substring(0,this.overviewDesc3.length() - 1)+"。";

			overviewDesc = ItextUtil.createParagraph(this.overviewDesc3, ItextUtil.createStyle(
					ItextUtil.FAMILY_TEXT, 12, ItextUtil.COLOR_BLACK, Font.NORMAL), Element.ALIGN_LEFT, 3, 3, 36);
			doc.add(overviewDesc);
		}

		/*
		 * 大数据 信息
		 */
		beansdl = query(word, query, time,"domain_level","中央重点,省级重点,地方城市");
		Paragraph infoNav = ItextUtil.createParagraph("二、重点新闻媒体关注热点", ItextUtil.createStyle(ItextUtil.FAMILY_TITLE, 18,
				ItextUtil.COLOR_BLACK, Font.BOLD), Element.ALIGN_LEFT, 10, 5);
		doc.add(infoNav);
		int num = 0;
		if(beansdl!=null&&beansdl.getSize()>0){
			for (int i = 0; i < beansdl.getSize(); i++) {
				Article bean = beansdl.getData().get(i).getDocs().get(0);
				if(bean!=null&&!bean.equals("")){
					String title = (i + 1) + ". " + bean.getTitle().replaceAll("<[/]?em>", ""), source = bean.getDomainSource(), pubdate = bean
							.getPubdate().toString(), summary = bean.getSummary().replaceAll("<[/]?em>", ""), href = bean.getUrl();
					String links;
					try {
						if(href.indexOf("https://")==-1){
							links = href.substring(href.indexOf("http://") + 7);
							links = links.substring(0, links.indexOf("/"));
						}else{
							links = href.substring(href.indexOf("https://") + 8);
							links = links.substring(0, links.indexOf("/"));
						}
					} catch (Exception e) {
						links = href;
					}
					// 标题
					Paragraph infoLinks1 = ItextUtil.createParagraph("", ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 14,
							ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 0, 36);
					Anchor anchor1 = ItextUtil.createAnchor(title, href, ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 12,
							ItextUtil.COLOR_BLUE));
					infoLinks1.add(anchor1);
					doc.add(infoLinks1);
//				Paragraph infoTitle = ItextUtil.createParagraph(title, ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 14,
//						ItextUtil.COLOR_BLACK, Font.BOLD), Element.ALIGN_LEFT, 5, 0, 36);
//				doc.add(infoTitle);
					// 来源
					Paragraph infoSource = ItextUtil.createParagraph("【来源】" + source + "   " + pubdate, ItextUtil.createStyle(
							ItextUtil.FAMILY_TEXT, 12, ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 0, 36);
					doc.add(infoSource);

					// 链接
					Paragraph infoLinks = ItextUtil.createParagraph("【链接】", ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 12,
							ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 0, 36);
					Anchor anchor = ItextUtil.createAnchor(links, href, ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 12,
							ItextUtil.COLOR_BLUE));
					infoLinks.add(anchor);
					doc.add(infoLinks);
					// 摘要
					Paragraph infoSummary = ItextUtil.createParagraph("【摘要】" + summary, ItextUtil.createStyle(
							ItextUtil.FAMILY_TEXT, 12, ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 10, 36);
					doc.add(infoSummary);
					num++;
					if(num==10)
						break;
				}

			}
		}
		/**
		 * 		政务网站关注热点
		 */
		beanssdzw = query(word, query, time, SearchFiled.MEDIA_TYPE.toLowerCase(), MediaType.GOVERNMENT.toString());
		Paragraph infoNavdm = ItextUtil.createParagraph("三、政务网站关注热点", ItextUtil.createStyle(ItextUtil.FAMILY_TITLE, 18,
				ItextUtil.COLOR_BLACK, Font.BOLD), Element.ALIGN_LEFT, 10, 5);
		doc.add(infoNavdm);
		num = 0;
		if(beanssdzw!=null&&beanssdzw.getSize()>0){
			for (int i = 0; i < beanssdzw.getSize(); i++) {
				Article bean = beanssdzw.getData().get(i).getDocs().get(0);
				if(bean!=null&&!bean.equals("")){
					String title = (i + 1) + ". " + bean.getTitle().replaceAll("<[/]?em>", ""), source = bean.getDomainSource(), pubdate = bean
							.getPubdate().toString(), summary = bean.getSummary().replaceAll("<[/]?em>", ""), href = bean.getUrl();
					String links;
					try {
						if(href.indexOf("https://")==-1){
							links = href.substring(href.indexOf("http://") + 7);
							links = links.substring(0, links.indexOf("/"));
						}else{
							links = href.substring(href.indexOf("https://") + 8);
							links = links.substring(0, links.indexOf("/"));
						}
					} catch (Exception e) {
						links = href;
					}
					// 标题
					Paragraph infoLinks1 = ItextUtil.createParagraph("", ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 14,
							ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 0, 36);
					Anchor anchor1 = ItextUtil.createAnchor(title, href, ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 12,
							ItextUtil.COLOR_BLUE));
					infoLinks1.add(anchor1);
					doc.add(infoLinks1);
//				Paragraph infoTitle = ItextUtil.createParagraph(title, ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 14,
//						ItextUtil.COLOR_BLACK, Font.BOLD), Element.ALIGN_LEFT, 5, 0, 36);
//				doc.add(infoTitle);
					// 来源
					Paragraph infoSource = ItextUtil.createParagraph("【来源】" + source + "   " + pubdate, ItextUtil.createStyle(
							ItextUtil.FAMILY_TEXT, 12, ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 0, 36);
					doc.add(infoSource);

					// 链接
					Paragraph infoLinks = ItextUtil.createParagraph("【链接】", ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 12,
							ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 0, 36);
					Anchor anchor = ItextUtil.createAnchor(links, href, ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 12,
							ItextUtil.COLOR_BLUE));
					infoLinks.add(anchor);
					doc.add(infoLinks);
					// 摘要
					Paragraph infoSummary = ItextUtil.createParagraph("【摘要】" + summary, ItextUtil.createStyle(
							ItextUtil.FAMILY_TEXT, 12, ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 10, 36);
					doc.add(infoSummary);
					num++;
					if(num==10)
						break;
				}

			}
		}
		/**
		 * 		微博关注热点
		 */
		beanssd = query(word, query, time,SearchFiled.MEDIA_TYPE.toLowerCase(),MediaType.WEIBO.toString());
		Paragraph infoNavsd = ItextUtil.createParagraph("四、微博关注热点", ItextUtil.createStyle(ItextUtil.FAMILY_TITLE, 18,
				ItextUtil.COLOR_BLACK, Font.BOLD), Element.ALIGN_LEFT, 10, 5);
		doc.add(infoNavsd);
		num = 0;
		if(beanssd!=null&&beanssd.getSize()>0){
			for (int i = 0; i < beanssd.getSize(); i++) {
				Article bean = beanssd.getData().get(i).getDocs().get(0);
				if(bean!=null&&!bean.equals("")){
					String title = (i + 1) + ". " + bean.getTitle().replaceAll("<[/]?em>", ""), source = bean.getDomainSource(), pubdate = bean
							.getPubdate().toString(), summary = bean.getSummary().replaceAll("<[/]?em>", ""), href = bean.getUrl();
					String links;
					try {
						if(href.indexOf("https://")==-1){
							links = href.substring(href.indexOf("http://") + 7);
							links = links.substring(0, links.indexOf("/"));
						}else{
							links = href.substring(href.indexOf("https://") + 8);
							links = links.substring(0, links.indexOf("/"));
						}
					} catch (Exception e) {
						links = href;
					}
					// 标题
					Paragraph infoLinks1 = ItextUtil.createParagraph("", ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 14,
							ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 0, 36);
					Anchor anchor1 = ItextUtil.createAnchor(title, href, ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 12,
							ItextUtil.COLOR_BLUE));
					infoLinks1.add(anchor1);
					doc.add(infoLinks1);
//				Paragraph infoTitle = ItextUtil.createParagraph(title, ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 14,
//						ItextUtil.COLOR_BLACK, Font.BOLD), Element.ALIGN_LEFT, 5, 0, 36);
//				doc.add(infoTitle);
					// 来源
					Paragraph infoSource = ItextUtil.createParagraph("【来源】" + source + "   " + pubdate, ItextUtil.createStyle(
							ItextUtil.FAMILY_TEXT, 12, ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 0, 36);
					doc.add(infoSource);

					// 链接
					Paragraph infoLinks = ItextUtil.createParagraph("【链接】", ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 12,
							ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 0, 36);
					Anchor anchor = ItextUtil.createAnchor(links, href, ItextUtil.createStyle(ItextUtil.FAMILY_TEXT, 12,
							ItextUtil.COLOR_BLUE));
					infoLinks.add(anchor);
					doc.add(infoLinks);
					// 摘要
					Paragraph infoSummary = ItextUtil.createParagraph("【摘要】" + summary, ItextUtil.createStyle(
							ItextUtil.FAMILY_TEXT, 12, ItextUtil.COLOR_BLACK), Element.ALIGN_LEFT, 0, 10, 36);
					doc.add(infoSummary);
					num++;
					if(num==10)
						break;
				}

			}
		}

		doc.close();
		return docPath;
	}
	public String createHtml() {
		ReportHtml html = new ReportHtml();
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("<div class=\"report-wrapper\"><div id=\"report\" class=\"report\">");
		if(overviewDesc1==null){
			overviewDesc1 = "";
		}
		if(overviewDesc3==null){
			overviewDesc3 = "";
		}
		if(overviewImgs==null){
			overviewImgs = "";
		}
		// 报告头部
		String head = html.createHead(title, number, subtitleLeft, subtitleRight);
		sBuilder.append(head);
		// 大数据综述
		String overview = html.createOverviewM("一、大数据综述", overviewDesc,overviewDesc1,overviewDesc2, overviewDesc3,overviewImgs);
		sBuilder.append(overview);
		// 大数据分布ss
		String pie = html.createPie("二、大数据分布", pieImgs);
		sBuilder.append(pie);
		sBuilder.append("<div class=\"desc\">" + overviewDesc3 + "</div>");
		String info = html.createInfo("二、重点新闻媒体关注热点", beansdl);
		sBuilder.append(info);
		String info1 = html.createInfo("三、政务网站关注热点", beanssdzw);
		sBuilder.append(info1);
		String info2 = html.createInfo("四、两微一端关注热点", beanssd);
		sBuilder.append(info2);
		// top10
//		String top = html.createTop("三、Top10", "一月参与人发表信息top10", topAu, "一月网站发表信息top10", topDm);
//		sBuilder.append(top);
		//String info = html.createInfo("四、热点大数据", beans);
		//sBuilder.append(info);
		sBuilder.append("</div></div>");
		return sBuilder.toString();
	}

	public Cubes trend(String word, String query, long[] time) throws Exception {
		Cubes cubes = null;
		try {
			Map<String,String> map = new HashMap();
			map.put("between",time[0]+","+time[1]);
			map.put("keywords",word);
			map.put("message_type","正文");
			map.put("group",GroupField.DAY.toString());
			map.put("group2",GroupField.EMOTION.toString());
			if(aloneReport.getTitle()!=null){
				map.put("title",aloneReport.getTitle());
			}
			if(aloneReport.getAuthor()!=null){
				map.put("author",aloneReport.getAuthor());
			}
			if(aloneReport.getWebattr()!=null){
				map.put("domain",aloneReport.getWebattr());
			}
			cubes = reportService.cube(map);
		} catch (Exception e) {
			System.out.println("检索搜索异常");
		} finally {
			return cubes;
		}

	}
	public Cubes trend1(String word, String query, long[] time) throws Exception {
		Cubes cubes = null;
		try {
			Map<String,String> map = new HashMap();
			map.put("keywords",word);
			map.put("message_type","正文");
			map.put("between",time[0]+","+time[1]);
			map.put("group",GroupField.DAY.toString());
			map.put("group2",GroupField.MEDIA_TYPE.toString());
			if(aloneReport.getTitle()!=null){
				map.put("title",aloneReport.getTitle());
			}
			if(aloneReport.getAuthor()!=null){
				map.put("author",aloneReport.getAuthor());
			}
			if(aloneReport.getWebattr()!=null){
				map.put("domain",aloneReport.getWebattr());
			}
			cubes = reportService.cube(map);
		} catch (Exception e) {
			System.out.println("检索搜索异常");
		} finally {
			return cubes;
		}

	}

	public Groups group(String word, String query, long[] time, String field) throws Exception {
		Groups beans = null;
		try {
			Map<String,String> map = new HashMap();
			map.put("keywords",word);
			map.put("message_type","正文");
			map.put("between",time[0]+","+time[1]);
			map.put("group",field);
			if(aloneReport.getTitle()!=null){
				map.put("title",aloneReport.getTitle());
			}
			if(aloneReport.getAuthor()!=null){
				map.put("author",aloneReport.getAuthor());
			}
			if(aloneReport.getWebattr()!=null){
				map.put("domain",aloneReport.getWebattr());
			}
			beans = reportService.groups(map);
		} catch (Exception e) {
			System.out.println("检索搜索异常");
		} finally {
			return beans;
		}

	}

//	public Cubes top10(String word, String query, long[] time, String field) throws Exception {
//        Map<String,String> map = new HashMap();
//        map.put("word",word);
//        map.put("message_type","正文");
//        map.put("between",time[0]+","+time[1]);
//        map.put("group",GroupField.EMOTION.toString());
//        map.put("max_group","10");
//		if(aloneReport.getTitle()!=null){
//			map.put("title",aloneReport.getTitle());
//		}
//		if(aloneReport.getAuthor()!=null){
//			map.put("author",aloneReport.getAuthor());
//		}
//		if(aloneReport.getWebattr()!=null){
//			map.put("domain",aloneReport.getWebattr());
//		}
//        Cubes cubes = reportService.cube(map);
//		return cubes;
//	}
	public Clusters query(String word, String query, long[] time) throws Exception {

		Clusters beans = null;
		try {
			Map<String,String> map = new HashMap();
			String mt = "微博正文,一般新闻,主题帖,微信正文";
			map.put("media_type",mt);
			map.put("keywords",word);
			map.put("message_type","正文");
			map.put("between",time[0]+","+time[1]);
			map.put("distance","0.5");
			if(aloneReport.getTitle()!=null){
				map.put("title",aloneReport.getTitle());
			}
			if(aloneReport.getAuthor()!=null){
				map.put("author",aloneReport.getAuthor());
			}
			if(aloneReport.getWebattr()!=null){
				map.put("domain",aloneReport.getWebattr());
			}
			beans = reportService.cluster(map);
		} catch (Exception e) {
			System.out.println("检索搜索异常");
		} finally {
			return beans;
		}

	}


	public String createHtmlword() {
		long[] time = new long[2];
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);
		time[1] = c.getTimeInMillis() - 1;
		c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1);
		time[0] = c.getTimeInMillis();
		StringBuilder sBuilder = new StringBuilder();
		String words="关键词云：";
		try{
		 words=queryword(word,query ,time);
		}
		catch(Exception e)
		{
			words="服务器关键词云中心发生异常，请通知客服修复该问题";
		}
		sBuilder.append(words);

		return sBuilder.toString();
	}
	public String createHtmlwordc() {
		long[] time = new long[2];
		//time[0] = DateUtil.getClearedTime(1);
		//time[1] = time[0] + 24 * 60 * 60 * 1000l - 1;
		time=DateUtil.getSearchTime("30");
		System.out.println("即时报告区间cloud："+time[0]+","+time[1]);
		StringBuilder sBuilder = new StringBuilder();
		String words="关键词云：";
		try{
		 words=queryword(word,query ,time);
		}
		catch(Exception e)
		{
			words="服务器关键词云中心发生异常，请通知客服修复该问题";
		}
		sBuilder.append(words);

		return words;
	}
public String queryword(String word, String query, long[] time) throws Exception {
		String cloud="(大数据摘要见附件)<br>";
		return cloud;
	}

	public Clusters query(String word, String query, long[] time, String fname, String  filed ) throws Exception {
		Clusters clusters = null;
		try {
			Map map = new HashMap();
			map.put(fname,filed);
			map.put("keywords",word);
			map.put("between",time[0]+","+time[1]);
//        map.put("message_type","正文");
			map.put("distance","0.5");
			map.put("max_group","20");
			if(aloneReport.getTitle()!=null){
				map.put("title",aloneReport.getTitle());
			}
			if(aloneReport.getAuthor()!=null){
				map.put("author",aloneReport.getAuthor());
			}
			if(aloneReport.getWebattr()!=null){
				map.put("domain",aloneReport.getWebattr());
			}
			clusters = reportService.cluster(map);
			System.out.println("beans123:"+clusters.getTotal());
		} catch (Exception e) {
			System.out.println("检索搜索异常");
		} finally {
			return clusters;
		}

	}


}
